set matsize 10000
global mainpath ".."

import delimited "$mainpath\output\bluechip_rule_regressions.csv", varnames(1) clear 

rename gamma_fe gamma_ffe
gen DATE=date(date,"YMD")
gen month=mofd(DATE)
rename month ym

order ym
tsset ym
*natural units for gamma
replace gamma_ffe=gamma_ffe/4
replace gamma_ols=gamma_ols/4

tempfile gamma_regs
save `gamma_regs'


use "$mainpath\data\bc_May23.dta", clear
gen ym = mofd(date)

*natural units for interest rates and inflation 
replace ff=ff/4
replace cpi=cpi/4

*there seems to be one typo in the data
replace ff=1.25 if ff==0.125&ym==421


tempfile coeffs_gap
statsby _b _se, by(ym) saving(`coeffs_gap', replace): areg ff cpi gap, absorb(id)

gen e_ff_ffe = .
gen e_ff = .
gen e_gap_ffe = .
gen e_ff_nocontrol_ffe=.
gen r2_ffe=.
gen r2=.

forvalues m = 300/760 {
		
		* MP residual, forecaster FE
		qui areg ff cpi gap if ym==`m', absorb(id)
		predict e, resid
		replace e_ff_ffe = e if ym==`m'
		drop e
		
		* MP residual, no forecaster FE
		qui reg ff cpi gap if ym==`m', ro
		predict e, resid
		replace e_ff = e if ym==`m'
		drop e
		
		* Output gap residual, forecaster FE
		qui areg gap if ym==`m', absorb(id)
		predict e, resid
		replace e_gap_ffe = e if ym==`m'
		drop e
		
		* Fed funds rate onto forecaster FE
		qui areg ff if ym==`m', absorb(id)
		predict e, resid
		replace e_ff_nocontrol_ffe = e if ym==`m'
		drop e
*	}
}

collapse (sd) e_ff_ffe e_gap_ffe e_ff gap e_ff_nocontrol_ffe, by(ym)
merge 1:1 ym using `coeffs_gap'
drop _merge

merge 1:1 ym using `gamma_regs'
drop _merge

rename e_ff_ffe SD_ff_ffe
rename e_ff SD_ff
rename e_gap_ffe SD_gap_ffe
rename gap SD_gap 
rename e_ff_nocontrol_ffe SD_nocontrol_ffe

gen VAR_ff_ffe = SD_ff_ffe^2
gen VAR_gap_ffe = SD_gap_ffe^2
gen VAR_gap = SD_gap^2
gen VAR_ff = SD_ff^2
gen r2_ffe=1-SD_ff_ffe^2/SD_nocontrol_ffe^2

* ratio for forecaster FE estimates
gen VAR_ff_ffe_VAR_gap_ffe = VAR_ff_ffe/VAR_gap_ffe

* ratio for pooled OLS estimates
gen VAR_ff_VAR_gap = VAR_ff/VAR_gap

format ym %tm
order ym gamma_ols VAR_ff_VAR_gap gamma_ffe VAR_ff_ffe_VAR_gap_ffe
br ym gamma_ols VAR_ff_VAR_gap gamma_ffe VAR_ff_ffe_VAR_gap_ffe

gen gamma_ols_bias_adj_eq14 = gamma_ols+1/(1+gamma_ols)*VAR_ff_VAR_gap
gen gamma_ols_bias_adj_c14_low_root = 1/2*(gamma_ols-1-sqrt((gamma_ols-1)^2+4*(VAR_ff_VAR_gap+gamma_ols))) 
gen gamma_ols_bias_adj_c14_high_root = 1/2*(gamma_ols-1+sqrt((gamma_ols-1)^2+4*(VAR_ff_VAR_gap+gamma_ols)))

gen gamma_ffe_bias_adj_eq14 = gamma_ffe+1/(1+gamma_ffe)*VAR_ff_ffe_VAR_gap_ffe
gen gamma_ffe_bias_adj_c14_low_root = 1/2*(gamma_ffe-1-sqrt((gamma_ffe-1)^2+4*(VAR_ff_ffe_VAR_gap_ffe+gamma_ffe))) 
gen gamma_ffe_bias_adj_c14_high_root = 1/2*(gamma_ffe-1+sqrt((gamma_ffe-1)^2+4*(VAR_ff_ffe_VAR_gap_ffe+gamma_ffe)))

* ignore low roots which are all negative
sum *low_root

corr gamma_ols gamma_ols_bias_adj_eq14 gamma_ols_bias_adj_c14_high_root
corr gamma_ffe gamma_ffe_bias_adj_eq14 gamma_ffe_bias_adj_c14_high_root

tsset ym
corr s.gamma_ols s.gamma_ols_bias_adj_eq14 s.gamma_ols_bias_adj_c14_high_root
corr s.gamma_ffe s.gamma_ffe_bias_adj_eq14 s.gamma_ffe_bias_adj_c14_high_root


* scale estimates back up
replace gamma_ols = gamma_ols*4
replace gamma_ffe = gamma_ffe*4
replace gamma_ols_bias_adj_c14_high_root = 4*gamma_ols_bias_adj_c14_high_root
replace gamma_ffe_bias_adj_c14_high_root = 4*gamma_ffe_bias_adj_c14_high_root

label var gamma_ffe "Gamma_FFE"
label var gamma_ffe_bias_adj_c14_high_root "Gamma_FFE bias adjusted"

twoway line gamma_ffe gamma_ffe_bias_adj_c14_high_root ym 
keep gamma_ffe gamma_ffe_bias_adj_c14_high_root ym 

save "$mainpath\output\gamma_bias_adjusted",replace